// Copyright (C) 2025, ZWSOFT Co., LTD.(Guangzhou)
// Licensed under the MIT License. See LICENSE in the project root for license information.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using TestClient.Domain;

namespace TestClient.ViewModel;
public partial class TrimTestViewModel : PageViewModelBase
{
    [ObservableProperty]
    private string curveId1 = "";
    [ObservableProperty]
    private string curveId2 = "";
    [ObservableProperty]
    private int curve1Keep = 0;
    [ObservableProperty]
    private int curve2Keep = 0;

    [ObservableProperty]
    private string surfaceId = "";
    [ObservableProperty]
    private string curveLoopIds = "";
    [ObservableProperty]
    private string resultSurfaceId = "";

    public IAsyncRelayCommand SelectCurve1Command { get; }
    public IAsyncRelayCommand SelectCurve2Command { get; }
    public IAsyncRelayCommand TestTrimCommand { get; }

    public IAsyncRelayCommand SelectSurfaceCommand { get; }
    public IAsyncRelayCommand SelectCurveLoopCommand { get; }
    public IAsyncRelayCommand TestSurfaceExtractCommand { get; }

    public TrimTestViewModel(VFAdapterWrapper wrapper, ISnackNotifyService notifyService) : base(wrapper, notifyService)
    {
        SelectCurve1Command = CreateAsyncRpcCommand(SelectCurve1);
        SelectCurve2Command = CreateAsyncRpcCommand(SelectCurve2);
        TestTrimCommand = CreateAsyncRpcCommand(TestTrim);
        SelectSurfaceCommand = CreateAsyncRpcCommand(SelectSurface);
        SelectCurveLoopCommand = CreateAsyncRpcCommand(SelectCurveLoop);
        TestSurfaceExtractCommand = CreateAsyncRpcCommand(TestSurfaceExtract);
    }

    private async Task SelectCurve1()
    {
        EnsureConnected();
        CurveId1 = await ViewModelHelpers.SelectEnt(Wrapper, "Curve");
    }
    private async Task SelectCurve2()
    {
        EnsureConnected();
        CurveId2 = await ViewModelHelpers.SelectEnt(Wrapper, "Curve");
    }
    private async Task TestTrim()
    {
        EnsureConnected();
        await Task.Run(() =>
        {
            Wrapper.CreateSplitOrTrim(CurveId1, CurveId2, "Trim", Curve1Keep + 1, Curve2Keep + 1);
        });
    }
    private async Task SelectSurface()
    {
        EnsureConnected();
        SurfaceId = await ViewModelHelpers.SelectEnt(Wrapper, "Surface");
    }
    private async Task SelectCurveLoop()
    {
        EnsureConnected();
        var curves = await Task.Run(() =>
        {
            return Wrapper.MultiSelect("Curve");
        });
        CurveLoopIds = string.Join(", ", curves);
    }
    private async Task TestSurfaceExtract()
    {
        EnsureConnected();
        var curveIds = CurveLoopIds.Split(',').Select(x => x.Trim()).ToArray();
        ResultSurfaceId = await Task.Run(() =>
        {
            return Wrapper.CreateSurfaceByFill(curveIds, SurfaceId);
        });
    }
}
